package com.jxccp.ui.view;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.jxccp.im.chat.common.config.Server;
import com.jxccp.im.chat.manager.JXImManager;
import com.jxccp.ui.JXAccountHelper;
import com.jxccp.ui.JXUiHelper;
import com.jxccp.ui.R;

import java.util.Set;

public class JXSettingActivity extends Activity implements View.OnClickListener {

    public static final String CONFIG_NAME = "jx_setting";

    public static final int JX_CONFIG_REQUEST_CODE = 10086;

    private EditText edtProtocol;

    private EditText edtHost;

    private EditText edtPort;

    private EditText edtCid;

    private EditText edtName;

    private EditText editSelfOrder;

    private CheckBox cbTransfer;

    private CheckBox cbEndsession;

    private CheckBox cbMessagebox;

    private CheckBox cbCommonQuestion;

    private CheckBox cbSendImg2robot;

    private CheckBox cbSendVoice2robot;

    private CheckBox cbRerequestAfterChangeChannelNo;

    private CheckBox cbSuborg;

    private SharedPreferences sharedPreferences;

    private int openOther = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jx_activity_jxsetting);

        edtProtocol = findViewById(R.id.edt_prot);
        edtHost = findViewById(R.id.edt_host);
        edtPort = findViewById(R.id.edt_port);
        edtCid = findViewById(R.id.edt_cid);
        edtName = findViewById(R.id.edt_name);
        editSelfOrder = findViewById(R.id.edt_self_order);

        cbTransfer = findViewById(R.id.cb_transfer);
        cbTransfer.setOnClickListener(this);
        cbEndsession = findViewById(R.id.cb_endsession);
        cbMessagebox = findViewById(R.id.cb_messagebox);
        cbCommonQuestion = findViewById(R.id.cb_common_question);
        cbSendImg2robot = findViewById(R.id.cb_send_img_to_robot);
        cbSendVoice2robot = findViewById(R.id.cb_send_voice_to_robot);
        cbRerequestAfterChangeChannelNo = findViewById(R.id.cb_re_request_after_change_channel_no);
        cbSuborg = findViewById(R.id.cb_suborg);

        boolean showSuborg = getIntent().getBooleanExtra("suborg", true);
        if (!showSuborg) {
            cbSuborg.setVisibility(View.GONE);
        }

        init();
    }

    public void init() {
        //服务器地址
        sharedPreferences = this.getSharedPreferences(CONFIG_NAME, this.MODE_PRIVATE);
        String protocal = TextUtils.isEmpty(sharedPreferences.getString("protocol", "")) ?
                JXImManager.Config.getInstance().getServerAddress().protocol :
                sharedPreferences.getString("protocol", "");
        String host = TextUtils.isEmpty(sharedPreferences.getString("host", "")) ?
                JXImManager.Config.getInstance().getServerAddress().ip :
                sharedPreferences.getString("host", "");
        String port = TextUtils.isEmpty(sharedPreferences.getString("port", "")) ?
                String.valueOf(JXImManager.Config.getInstance().getServerAddress().port) :
                sharedPreferences.getString("port", "");
        edtProtocol.setText(protocal);
        edtHost.setText(host);
        edtPort.setText(port);

        //crm客户信息
        String cid = sharedPreferences.getString("cid", "");
        String name = sharedPreferences.getString("name", "");
        edtCid.setText(cid);
        edtName.setText(name);

        //ui 配置
        //设置转人工按钮可见
        boolean showRobotTransfer = sharedPreferences.getBoolean("showRobotTransfer", true);
        cbTransfer.setChecked(showRobotTransfer);

        //设置关闭会话按钮可见
        boolean showEndSession = sharedPreferences.getBoolean("showEndSession", true);
        cbEndsession.setChecked(showEndSession);

        //设置消息箱按钮可见
        boolean messageboxEnable = sharedPreferences.getBoolean("messageBox", true);
        cbMessagebox.setChecked(messageboxEnable);

        //设置使用常见问题（目前只在灰度上线，需要手动设置）
        boolean useCommonQuestion = sharedPreferences.getBoolean("useCommonQuestion", false);
        cbCommonQuestion.setChecked(useCommonQuestion);

        //是否允许发图片给机器人
        boolean sendImgToRobotEnable = sharedPreferences.getBoolean("sendImgToRobot", false);
        cbSendImg2robot.setChecked(sendImgToRobotEnable);

        //是否允许发语音给机器人
        boolean sendVoiceToRobotEnable = sharedPreferences.getBoolean("sendVoiceToRobot", false);
        cbSendVoice2robot.setChecked(sendVoiceToRobotEnable);

        //切换渠道号后重新请求
        boolean reRequestAfterChangeChannelNo = sharedPreferences.getBoolean("reRequest", false);
        cbRerequestAfterChangeChannelNo.setChecked(reRequestAfterChangeChannelNo);

        //是否显示子组织页面按钮
        boolean suborg = sharedPreferences.getBoolean("suborg", false);
        cbSuborg.setChecked(suborg);

        String selforderUrl = sharedPreferences.getString("selfOrderUri", "");
        editSelfOrder.setText(selforderUrl);
    }

    @Override
    protected void onPause() {
        //服务器地址
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String protocal = edtProtocol.getText().toString();
        String host = edtHost.getText().toString();
        String port = edtPort.getText().toString();
        editor.putString("protocol", protocal);
        editor.putString("host", host);
        editor.putString("port", port);

        //crm客户信息
        String cid = edtCid.getText().toString();
        String name = edtName.getText().toString();
        editor.putString("cid", cid);
        editor.putString("name", name);

        //其他
        String selfOrderUri = editSelfOrder.getText().toString();
        editor.putString("selfOrderUri",selfOrderUri);

        //ui配置
        editor.putBoolean("showRobotTransfer", cbTransfer.isChecked());
        editor.putBoolean("showEndSession", cbEndsession.isChecked());
        editor.putBoolean("messageBox", cbMessagebox.isChecked());
        editor.putBoolean("useCommonQuestion", cbCommonQuestion.isChecked());
        editor.putBoolean("sendImgToRobot", cbSendImg2robot.isChecked());
        editor.putBoolean("sendVoiceToRobot", cbSendVoice2robot.isChecked());
        editor.putBoolean("reRequest", cbRerequestAfterChangeChannelNo.isChecked());

        editor.putBoolean("suborg", cbSuborg.isChecked());

        editor.commit();
        setResult(RESULT_OK);
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.cb_transfer) {
            openOther++;
            if (openOther > 5){
                editSelfOrder.setVisibility(View.VISIBLE);
            }
        }
    }
}
